#include "interface.h"

#include <QKeyEvent>
#include <QGridLayout>

RealNumTextEdit::RealNumTextEdit(QWidget* parent /*= NULL*/) : QTextEdit(parent)
{
  setTabChangesFocus(true);
  setWordWrapMode(QTextOption::NoWrap);
  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  setFixedHeight(sizeHint().height());
}
RealNumTextEdit::RealNumTextEdit(double val, QWidget* parent /*= NULL*/) : QTextEdit(QString::number(val), parent)
{
  setTabChangesFocus(true);
  setWordWrapMode(QTextOption::NoWrap);
  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  setFixedHeight(sizeHint().height());
}

void RealNumTextEdit::setValue(double val)
{
  setText(QString::number(val));
}

void RealNumTextEdit::keyPressEvent(QKeyEvent *event)
{
  if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
    emit enterPressed();
  else
    QTextEdit::keyPressEvent(event);
}
QSize RealNumTextEdit::sizeHint() const
{
  QFontMetrics fm(font());
  int h = qMax(fm.height(), 14) + 4;
  int w = fm.width(QLatin1Char('x')) * 17 + 4;
  QStyleOptionFrameV2 opt;
  opt.initFrom(this);
  return (style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(w, h), this));
}

////////////////////////////
//InputStringDialog
////////////////////////////
InputStringDialog::InputStringDialog(QString title, QWidget* parent /*= NULL*/) : QDialog(parent)
{
  setWindowTitle(title);
  setButton = new QPushButton("OK", this);
  textEdit = new RealNumTextEdit(this);

  QGridLayout* pLayout = new QGridLayout();
  pLayout->addWidget(textEdit, 0, 0, 1, 2);
  pLayout->addWidget(setButton, 0, 2, 1, 1);
  setLayout(pLayout);
  connect(setButton, SIGNAL(clicked()), this, SLOT(set()));
}

void InputStringDialog::setString(QString data)
{
  str = data;
  textEdit->setText(str);
}
QString InputStringDialog::getString() const
{
  return str;
}

void InputStringDialog::set()
{
  str = textEdit->toPlainText();
  emit stringChanged();
}



